package service.impl;

import Dao.UserDao;
import Dao.impl.UserDaoImpl;
import entity.Orders;
import entity.Ticket;
import entity.User;
import service.BaseService;
import service.UserService;
import untils.Msg;
import untils.ResultPage;
import untils.SqlCollection;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    private SqlCollection sqlCollection = new SqlCollection();

    @Override
    public Msg<ArrayList<Orders>> findPaymentOrder(int user_id) {
        return userDao.findPaymentOrder(user_id);
    }

    @Override
    public Msg<ArrayList<Orders>> findNotPaymentOrder(int userId) {
        return userDao.findNotPayment(userId);
    }

    @Override
    public void deleteOrder(int orderId) {
        userDao.deleteOrder(orderId);
    }

    @Override
    public void deleteTickets(int ticketId) {
        userDao.deleteTickets(ticketId);
    }

    @Override
    public ArrayList<User> findUserData(int userId) {

        return userDao.findUserData(userId);
    }

    @Override
    public void addUserData(int userId,String uName, String otherphone,String idCard) {

        userDao.addUserData(userId,uName,otherphone,idCard);
    }

    @Override
    public ArrayList<Ticket> findMyTickets(String idCard) {
        return userDao.findMyTickets(idCard);
    }

    @Override
    public void lookExe() {
        // 指定要读取的txt文件的路径
        String filePath = "file\\tongzhi.txt";

        // 使用try-with-resources语句自动关闭资源
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            // 逐行读取文件内容
            while ((line = reader.readLine()) != null) {
                // 打印每一行
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件读取失败，请检查文件路径是否正确");
        }
    }

    @Override
    public ResultPage<User> findPageUserList(int currentPage) {
        //1、创建ResultPage 对象 page
        ResultPage<User> page = new ResultPage<>();
        //2、把当前页设置到page 对象中
        page.setCurrentPage(currentPage);
        //3、设置每页展示的条数  pageSize
        int pageSize = 2;
        //4、计算起始条数  : firstCount = (currentPage-1）*pageSize
        int firstCount = (currentPage-1)*pageSize;
        //5、调用dao层中的方法，查询分页结果  参数: firstCount  pageSize  返回值：ArrayList<StudentEntity>
        ArrayList<User> users = userDao.findPageUserList(firstCount,pageSize);
        //6、把查询到的列表设置到page对象中
        page.setLists(users);
        //7、调用dao层中的方法，查询总条数 参数：无   返回值 ： int
        int totalCount = userDao.findStudentTolalCount();
        //8、把总条数设置到page对象中
        page.setTotalCount(totalCount);
        //9、根据总条数计算总页数  总条数%每页展示条数==0? 总条数/每页展示条数：总条数/每页展示条数+1
        int totalPage = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
        //10、把总页数设置到page对象中
        page.setTotalPage(totalPage);
        //11、返回值ResultPage
        return page;
    }
}
